﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;
using QuanLyBanHang.VO;

namespace QuanLyBanHang
{
    public partial class Form1 : Form
    {
        public bool them = false;
        int manv;
        NhanVien_DAL nv_dal = new NhanVien_DAL();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
            dataGridView1.ReadOnly = true;
            dataGridView1.Columns[0].HeaderText = "Mã nhân viên";
            dataGridView1.Columns[1].HeaderText = "Họ Tên";
            dataGridView1.Columns[2].HeaderText = "Lương";
            dataGridView1.Columns[3].HeaderText = "Địa chỉ";
            dataGridView1.Columns[4].HeaderText = "Số điện thoại";
            dataGridView1.Columns[5].HeaderText = "Tài khoản";
            dataGridView1.Columns[6].HeaderText = "Mật khẩu";
            dataGridView1.Columns[7].HeaderText = "Quyền";
        }

        private void button_tk_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            them = true;
            button_add.Enabled = false;
            button_edit.Enabled = false;
            button_cancel.Enabled = true;
            button_del.Enabled = false;
            button_save.Enabled = true;

            lbl_Ten.Visible = true;
            lbl_Luong.Visible = true;
            lbl_DT.Visible = true;
            lbl_DC.Visible = true;
            lbl_TaiKhoan.Visible = true;
            lbl_MK.Visible = true;
            rbtn_Admin.Visible = true;
            rbtn_User.Visible = true;
            rbtn_User.Checked = true;
            if (rbtn_Admin.Checked == true)
            {
                rbtn_User.Checked = false;
            }
            else if (rbtn_User.Checked == true)
            {
                rbtn_Admin.Checked = false;
            }
            txt_Ten.Visible = true;
            txt_TaiKhoan.Visible = true;
            txt_MatKhau.Visible = true;
            txt_Luong.Visible = true;
            txt_DT.Visible = true;
            txt_DiaChi.Visible = true;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            button_add.Enabled = false;
            button_edit.Enabled = false;
            button_cancel.Enabled = true;
            button_del.Enabled = false;
            button_save.Enabled = true;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[7].ReadOnly = true;
            manv = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (them)
            {
                int quyen;
                if (rbtn_Admin.Checked == true)
                    quyen = 1;
                else quyen = 0;
                //int n = dataGridView1.NewRowIndex - 1;
                //string ten = dataGridView1.Rows[n].Cells[1].Value.ToString();
                //int luong = int.Parse(dataGridView1.Rows[n].Cells[2].Value.ToString());
                //string dc = dataGridView1.Rows[n].Cells[3].Value.ToString();
                //string sodt = dataGridView1.Rows[n].Cells[4].Value.ToString();
                //string tk = dataGridView1.Rows[n].Cells[5].Value.ToString();
                //string matkhau = dataGridView1.Rows[n].Cells[6].Value.ToString();
                //int quyen = int.Parse(dataGridView1.Rows[n].Cells[7].Value.ToString());
                if (nv_dal.insert(txt_Ten.Text, int.Parse(txt_Luong.Text), txt_DiaChi.Text, txt_DT.Text, txt_TaiKhoan.Text, txt_MatKhau.Text, quyen)) MessageBox.Show("Thành công");
                else MessageBox.Show("Có lỗi");

            }
            else
            {
                int n = dataGridView1.CurrentRow.Index;
                //MessageBox.Show(n.ToString());
                string ten = dataGridView1.Rows[n].Cells[1].Value.ToString();
                int luong = int.Parse(dataGridView1.Rows[n].Cells[2].Value.ToString());
                string dc = dataGridView1.Rows[n].Cells[3].Value.ToString();
                string sodt = dataGridView1.Rows[n].Cells[4].Value.ToString();
                string tk = dataGridView1.Rows[n].Cells[5].Value.ToString();
                string matkhau = dataGridView1.Rows[n].Cells[6].Value.ToString();
                int quyen = int.Parse(dataGridView1.Rows[n].Cells[7].Value.ToString());
                if (nv_dal.update(manv, ten, luong, dc, sodt, tk, matkhau, quyen)) MessageBox.Show("Thành công");
                else MessageBox.Show("Có lỗi");
            }
            them = false;
            button_add.Enabled = true;
            button_edit.Enabled = true;
            button_cancel.Enabled = false;
            button_del.Enabled = true;
            button_save.Enabled = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            them = false;
            button_add.Enabled = true;
            button_edit.Enabled = true;
            button_cancel.Enabled = false;
            button_del.Enabled = true;
            button_save.Enabled = false;
            dataGridView1.ReadOnly = true;
        }

        private void button_del_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            if (nv_dal.delete(id)) MessageBox.Show("Đã xóa");
            else MessageBox.Show("Có lỗi");
            dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_Luong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_DT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}
